#include "array.h"
#include <algorithm>

void Array::swap(Array& arr) {
	std::swap(_size, arr._size);
	std::swap(capacity, arr.capacity);
	std::swap(a, arr.a);
}

Array::Array(size_t size): _size(0), capacity(size) {
	a = new int[size];
}

Array::Array(const Array& arr): _size(arr.size()), capacity(arr.size()) {
	_size = arr.size();
	a = new int[_size];

	for (int i = 0; i < _size; i++) {
		a[i] = arr.get(i);
	}
}

Array& Array::operator=(const Array& arr) {
	if (this == &arr) {
		return *this;
	}

	Array tmp(arr);
	swap(tmp);
	return *this;
}

int Array::get(size_t index) const {
	return a[index];
}

void Array::set(size_t index, int value) {
	a[index] = value;
}

void Array::push_back(int value) {
	if (_size < capacity) {
		a[_size++] = value;
	} else {
		capacity *= 2;
		int *newA = new int[capacity];
		for (int i = 0; i < _size; i++) {
			newA[i] = a[i];
		}
		delete[] a;
		a = newA;
		push_back(value);
	}
}

size_t Array::size() const {
	return _size;
}